package com.qf.controller;


import com.qf.annotation.RequiresPermissionsDesc;
import com.qf.pojo.DtsRole;
import com.qf.service.DtsPermissionService;
import com.qf.service.DtsRoleService;
import com.qf.util.Permission;
import com.qf.util.PermissionUtil;
import com.qf.util.ResponseUtil;
import com.qf.vo.PermVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.*;

import java.util.*;

import java.util.stream.Collectors;

@RestController
@RequestMapping("/admin/role")
public class AdminRoleController {

    @Autowired
    DtsRoleService roleService;
    @Autowired
    DtsPermissionService permissionService;
    @Autowired
    ApplicationContext context;

    @PostMapping("/permissions")
    public Object updatePermissions(@RequestBody Map<String,Object> map){
        int roleId = Integer.parseInt(String.valueOf(map.get("roleId")));
        List<String> permissions = (List<String>) map.get("permissions");

        //判断是否为超级管理员
        if (roleService.isSuperAdmin(roleId)){
            permissions = new ArrayList<>();
            //添加所有权限
            permissions.add("*");
        }

        permissionService.updatePermissions(roleId,permissions);

        return ResponseUtil.ok();
    }

    @GetMapping("/permissions")
    public Object permissions(Integer roleId){
        //
        List<Permission> permissionList = PermissionUtil.listPermission(context, "com.qf.controller");
        //获取系统权限
        List<PermVo> permVos = PermissionUtil.listPermVo(permissionList);

        boolean flag = roleService.isSuperAdmin(roleId);

        //已有权限
        Set<String> assignedPermissions = null;

        //判断是否是超级管理员
        //是则拥有所有权限
        if (flag){
            assignedPermissions = PermissionUtil.listPermissionString(permissionList);
        }
        //不是超级管理员则查找对应权限
        else {
            assignedPermissions = permissionService.findPermsByIds(new Integer[]{roleId});
        }

        Map<String,Object> data = new HashMap<>();
        data.put("systemPermissions",permVos);
        data.put("assignedPermissions",assignedPermissions);

        return ResponseUtil.ok(data);
    }

    @RequiresPermissions("admin:role:delete")
    @RequiresPermissionsDesc(menu = { "系统管理", "角色管理" }, button = "角色删除")
    @PostMapping("/delete")
    public Object delete(@RequestBody DtsRole role){
        roleService.delete(role);

        return ResponseUtil.ok();
    }

    @RequiresPermissions("admin:role:update")
    @RequiresPermissionsDesc(menu = { "系统管理", "角色管理" }, button = "角色编辑")
    @PostMapping("/update")
    public Object update(@RequestBody DtsRole role){
        roleService.update(role);

        return ResponseUtil.ok();
    }

    @RequiresPermissions("admin:role:create")
    @RequiresPermissionsDesc(menu = { "系统管理", "角色管理" }, button = "角色添加")
    @PostMapping("/create")
    public Object create(@RequestBody DtsRole role){
        roleService.insert(role);

        return ResponseUtil.ok();
    }

    @RequiresPermissions("admin:role:list")
    @RequiresPermissionsDesc(menu = { "系统管理", "角色管理" }, button = "角色查询")
    @GetMapping("/list")
    public Object list(@RequestParam(value = "page",required = false,defaultValue = "1")Integer page,
                       @RequestParam(value = "limit",required = false,defaultValue = "10")Integer limit,
                       @RequestParam(value = "sort",required = false,defaultValue = "add_time")String sort,
                       @RequestParam(value = "order",required = false,defaultValue = "desc")String order,
                       String rolename){

        Map<String,Object> data = roleService.findByPage(page,limit,sort,order,rolename);
        return ResponseUtil.ok(data);
    }

    @RequestMapping("/options")
    public Object option(){
        List<DtsRole> list =roleService.findAll();

        //数据转换
        //lambda表达式
        List<Map<String, Object>> data = list.stream().map(role -> {
            Map<String, Object> map = new HashMap<>();
            map.put("label", role.getName());
            map.put("value", role.getId());
            return map;
        }).collect(Collectors.toList());

        return ResponseUtil.ok(data);
    }
}
